package com.idconfirmsystem.activity;

import org.dom4j.DocumentException;
import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.ui.BindView;

import u.aly.bg;
import u.aly.br;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dtr.zxing.activity.CaptureActivity;
import com.idconfirmsystem.R;
import com.idconfirmsystem.lib.StringUtil;
import com.idconfirmsystem.model.BackModel;
import com.idconfirmsystem.model.BackModelList;
import com.idconfirmsystem.model.BagVerifyInfo;
import com.idconfirmsystem.model.BagVerifyUp;
import com.idconfirmsystem.model.QueryRet;
import com.idconfirmsystem.model.UserBean;
import com.idconfirmsystem.remote.ConfirmApi;
import com.idconfirmsystem.util.XmlUtils;
import com.umeng.analytics.d;

public class BagVerifyActivity extends KJActivity {
	private BagVerifyInfo bagInfo;
	private String serviceMethod="BankService/addVerifyBag";
	@BindView(id = R.id.sp_isempty)
	private Spinner isEmptySpinner;
	@BindView(id = R.id.tv_branch_num)
	private TextView tvBranchNum;
	@BindView(id = R.id.et_bag_num)
	private EditText tvBagNum;
	@BindView(id = R.id.et_bag_seal)
	private EditText tvBagSeal;

	@BindView(id = R.id.btn_check, click = true)
	private Button btnCheck;
	@BindView(id = R.id.btn_cancel, click = true)
	private Button btnCancel;
	@BindView(id = R.id.btn_scan, click = true)
	private Button btnScan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		bagInfo = (BagVerifyInfo) getIntent().getSerializableExtra("bag");
		if (bagInfo != null) {
			tvBagNum.setText(bagInfo.getBags_number());
			tvBagSeal.setText(bagInfo.getBags_seal_no());
			tvBranchNum.setText(bagInfo.getBranch_number());
		}
	}

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_bag_verify);

	}

	@Override
	public void initWidget() {
		// TODO Auto-generated method stub
		super.initWidget();

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.btn_cancel:
			this.finish();
			break;
		case R.id.btn_check:
			datatoserver();
			break;
		case R.id.btn_scan:
			Intent intent = new Intent(BagVerifyActivity.this, CaptureActivity.class);
			startActivityForResult(intent, 1003);
			break;
		default:
			break;
		}
	}

	public void datatoserver() {
		String bgnum = tvBagNum.getText().toString();
		if (StringUtil.isEmpty(bgnum)) {
			toast("箱包编号不能为空");
			return;
		}
		String bagseal = tvBagSeal.getText().toString();
		if (StringUtil.isEmpty(bagseal)) {
			toast("箱包签封号不能为空");
			return;
		}
//		toast("核验成功");
		BagVerifyUp vo=new BagVerifyUp();
		vo.setBagSealNo(bagseal);
		vo.setBagsNumber(bgnum);
		vo.setBranchNo(bagInfo.getBranch_number());
		vo.setLoginUserId(UserBean.getInstance().getUserName());
		
		ConfirmApi.pushData(vo, serviceMethod, callBack);
//		this.finish();
	}
	HttpCallBack callBack=new HttpCallBack() {

		@Override
		public void onFailure(int errorNo, String strMsg) {
			// TODO Auto-generated method stub
			super.onFailure(errorNo, strMsg);
			toast(errorNo+":"+strMsg);
		}

		@Override
		public void onSuccess(String t) {
			// TODO Auto-generated method stub
			super.onSuccess(t);
			try {
				QueryRet<BackModelList> ret = XmlUtils.toBean(BackModelList.class, t);
				BackModel ss = ret.getResult().getBackModels().get(0);
				toast(ss.getAae317());
				Log.d("bagverify", ss.getAae314());
				if(Integer.parseInt(ss.getAae314())==1){//核对成功则返回，失败则不管
					BagVerifyActivity.this.finish();
				}
			} catch (DocumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	};
	
	public void toast(String msg) {
		Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data==null) {
			return;
		}
		String zcode = data.getStringExtra("zcode");
		if (requestCode == 1003) {
			tvBagSeal.setText(zcode);
		}
	}
}
